<?php


function redirect($path)
{
	?>
	<script>
		window.location="<?php echo $path; ?>";
	</script>
	<?php
}

function convertDateToTime($date="01/01/1970",$flag='start')
{
	if(trim($date)!="")
	{
		$datearr=explode("/",$date);
        if(count($datearr)==3)
        {
    		if($flag=='start')
            {
                $nowtimedate=mktime(0,0,1,$datearr[1],$datearr[0],$datearr[2]);    
            }
            else
            {
                $nowtimedate=mktime(23,59,59,$datearr[1],$datearr[0],$datearr[2]);    
            }
             
    		return $nowtimedate;
        }
        else
        {
            return "";
        }
	}
	else
	{
		return "";
	}
}

function convertdate($datestr="")
{
	$datearr=explode("-",$datestr);
	if($datearr[2]!="0000")
	{
		return ($datearr[2]."/".$datearr[1]."/".$datearr[0]);
	}
	else
	{
		return "NA";
	}
}

function getuniquearraybasedvalues($nonuniquearr=array())
{
    $uniquearr=array();
    
    for($i=0;$i<count($nonuniquearr);$i++)
    {
        $flag=0;
        for($j=($i+1);$j<count($nonuniquearr);$j++)
        {
            if(trim($nonuniquearr[$i])==trim($nonuniquearr[$j]))
            {
                $flag=1;
                break;
            }
        }
        if($flag==0)
        {
            $uniquearr[]=$nonuniquearr[$i];
        }
    }
    return $uniquearr;
}

function getTimestamp($time)
{
	$arr_time=explode(".",$time);
	$total_sec=$arr_time[0]*60*60;
	if(isset($arr_time[1]))
	{
		$total_sec+=($arr_time[1]*60);
	}
	return $total_sec;
}
function convertTimeInNormal($time)
{
	$outstrtime=$time;
	$outstrtime=floatval(str_ireplace(":",".",$outstrtime));
	if($outstrtime>24)
	{
		$outstrtime=$outstrtime-24;
	}
	$outstrtime=str_ireplace(".",":",strval($outstrtime));
	if(!strstr($outstrtime,":"))
	{
		$outstrtime=$outstrtime.":00";
	}
	if(strstr($outstrtime,":3"))
	{
		$outstrtime=$outstrtime."0";
	}
	return $outstrtime;
}

function make_dropdown($arr_shifts,$id,$placeholdermsg,$name="",$selectedvalues="",$onchangefunction="javascript:void(0);")
{
	if($name=="")
	{
		$name=$id; 
	}
	$returnstr="";
	$returnstr.='<select id="'.$id.'" name="'.$name.'" onchange="'.$onchangefunction.'">
		<option value="">'.$placeholder.'</option>
		';
		if(count($arr_shifts)>0)
		{
			for($i=0;$i<count($arr_shifts);$i++)
			{
				if($selectedvalues!="")
				{
					if(stristr($arr_shifts[$i],$selectedvalues))
						$returnstr.='<option value="'.$arr_shifts[$i].'" selected>'.$arr_shifts[$i].'</option>';
					else
						$returnstr.='<option value="'.$arr_shifts[$i].'" >'.$arr_shifts[$i].'</option>';
				}
				else
				{
					$returnstr.='<option value="'.$arr_shifts[$i].'" >'.$arr_shifts[$i].'</option>';
				}
			}
		}
		
	$returnstr.='</select>';
	return $returnstr;
	?>
	
	<?php
}

function createinsertquerybasedonparams($tables="users",$fieldarray=array())
{
    $sqlquery="insert into ".$tables." set ";
    if(count($fieldarray)>0)
    {
        foreach($fieldarray as $key=>$value)
		{
			$sqlquery.=$key."=:".$key.",";
		}
        $sqlquery=substr($sqlquery,0,strlen($sqlquery)-1);
    }
    return $sqlquery;
}

function getbindarray($fieldarray=array())
{
    $bindarray=array();
    foreach($fieldarray as $key=>$value)
	{
		$bindarray[":".$key]=$value;
	}
    return $bindarray;
}

function createupdatequerybasedonparams($tables="users",$fieldarray=array(),$conditionarray=array())
{
    $sqlquery="update ".$tables." set ";
    if(count($fieldarray)>0)
    {
        foreach($fieldarray as $key=>$value)
		{
			$sqlquery.=$key."=:".$key.",";
		}
        $sqlquery=substr($sqlquery,0,strlen($sqlquery)-1);
    }
    if(count($conditionarray)>0)
    {
        $sqlquery.=" where ";
        foreach($conditionarray as $key=>$value)
		{
			$sqlquery.=$key."=:".$key." and ";
		}
        $sqlquery=substr($sqlquery,0,strlen($sqlquery)-4);
    }
    
    return $sqlquery;
}

function createdeletebasedonparams($tables="users",$conditionarray=array())
{
    $sqlquery="delete from ".$tables." ";
    if(count($conditionarray)>0)
    {
        $sqlquery.=" where ";
        foreach($conditionarray as $key=>$value)
		{
			$sqlquery.=$key."=:".$key." and ";
		}
        $sqlquery=substr($sqlquery,0,strlen($sqlquery)-4);
    }
    
    return $sqlquery;
}

?>